﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace QLKHACHHANG.DataAccess
{
    class DBCustomer
    {
        //Ham update thong tin khach hang
        public static bool UpdateCustomer(string Makh, string Tenkh, string Tenkhac, string Maloaikh, string Masothue,
                                          string Diachigiaodich, string Nguoilienhe, string Madanhxung, string Tenvitri,
                                          DateTime Ngaysinh, string Socmnd, string Sodienthoai, string Email, string Fax,
                                          string Ghichu)
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
            bool status = false;
            try
            {
                string connectionstring = SetConfig.getAppSetting("ConnectionString");
                conn = new SqlConnection(connectionstring);
                SqlCommand cmd = new SqlCommand("sp_SuaKhachHang", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Makh", Makh);
                cmd.Parameters.AddWithValue("@Tenkh", Tenkh);
                cmd.Parameters.AddWithValue("@Tenkhac", Tenkhac);
                cmd.Parameters.AddWithValue("@Maloaikh", Maloaikh);
                cmd.Parameters.AddWithValue("@Masothue", Masothue);
                cmd.Parameters.AddWithValue("@Diachigiaodich", Diachigiaodich);
                cmd.Parameters.AddWithValue("@Nguoilienhe", Nguoilienhe);
                cmd.Parameters.AddWithValue("@Madanhxung", Madanhxung);
                cmd.Parameters.AddWithValue("@Tenvitri", Tenvitri);
                cmd.Parameters.AddWithValue("@Ngaysinh", Ngaysinh);
                cmd.Parameters.AddWithValue("@Socmnd", Socmnd);
                cmd.Parameters.AddWithValue("@Sodienthoai", Sodienthoai);
                cmd.Parameters.AddWithValue("@Email", Email);
                cmd.Parameters.AddWithValue("@Fax", Fax);
                cmd.Parameters.AddWithValue("@Ghichu", Ghichu);
                conn.Open();
                rdr = cmd.ExecuteReader();
                status = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Vui lòng kiểm tra lại kết nối CSDL !\n\n" + "(Mã lỗi:" + ex.Message + ")");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
            return status;
        }
        // chen 1 khach hang vao csdl    
        public static bool InsertCustomer(string Makh, string Tenkh, string Tenkhac, string Maloaikh, string Masothue,
                                          string Diachigiaodich, string Nguoilienhe, string Madanhxung, string Tenvitri,
                                          DateTime Ngaysinh, string Socmnd, string Sodienthoai, string Email, string Fax,
                                          string Ghichu)
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
            bool status = false;
            try
            {
                string connectionstring = SetConfig.getAppSetting("ConnectionString");
                conn = new SqlConnection(connectionstring);
                SqlCommand cmd = new SqlCommand("sp_ThemKhachHang", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Makh", Makh);
                cmd.Parameters.AddWithValue("@Tenkh", Tenkh);
                cmd.Parameters.AddWithValue("@Tenkhac", Tenkhac);
                cmd.Parameters.AddWithValue("@Maloaikh", Maloaikh);
                cmd.Parameters.AddWithValue("@Masothue", Masothue);
                cmd.Parameters.AddWithValue("@Diachigiaodich", Diachigiaodich);
                cmd.Parameters.AddWithValue("@Nguoilienhe", Nguoilienhe);
                cmd.Parameters.AddWithValue("@Madanhxung", Madanhxung);
                cmd.Parameters.AddWithValue("@Tenvitri", Tenvitri);
                cmd.Parameters.AddWithValue("@Ngaysinh", Ngaysinh);
                cmd.Parameters.AddWithValue("@Socmnd", Socmnd);
                cmd.Parameters.AddWithValue("@Sodienthoai", Sodienthoai);
                cmd.Parameters.AddWithValue("@Email", Email);
                cmd.Parameters.AddWithValue("@Fax", Fax);
                cmd.Parameters.AddWithValue("@Ghichu", Ghichu);
                conn.Open();
                rdr = cmd.ExecuteReader();
                status = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Vui lòng kiểm tra lại kết nối CSDL !\n\n" + "(Mã lỗi:" + ex.Message + ")");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
            return status;
        }
        // xoa 1 khach hang tu csdl
        public static bool DeleteCustomer(string Makh)
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
            bool status = false;
            try
            {
                string connectionstring = SetConfig.getAppSetting("ConnectionString");
                conn = new SqlConnection(connectionstring);
                SqlCommand cmd = new SqlCommand("sp_XoaKhachHang", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Makh", Makh);               
                conn.Open();
                rdr = cmd.ExecuteReader();
                status = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Vui lòng kiểm tra lại kết nối CSDL !\n\n" + "(Mã lỗi:" + ex.Message + ")");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
            return status;
        }
    }
}
